<?php
namespace app\index\controller;
use PHPMailer\Mail;
use app\index\model\Sign;
use app\index\model\User as UserModel;
use think\Controller;
use think\Loader;
class User extends Common
{
    public function index()
    {
        $this->assign('title',"用户中心");
    	// var_dump(session(''));
        return $this->fetch();
    }

    public function active()
    {
        $user = UserModel::get(session('user_id'));
        $this->assign('user',$user);
        return $this->fetch();
    }

    public function check()
    {
        var_dump(input('token'));
    }

    public function send()
    {
        $user = UserModel::get(session('user_id'));

        if($user->is_active){
            $this->error('邮件已激活，不需要重新激活');
        }

        $token = md5($user->user_id.$user->nickname.mt_rand(0,999));

        $nickname = $user->nickname;

        $url = config('site_url').url('index/user/check',['token'=>$token]);

        $content =<<<xbs
        <div><includetail><table border="0" cellpadding="0" cellspacing="0" style="width: 600px; border: 1px solid #ddd; border-radius: 3px; color: #555; font-family: 'Helvetica Neue Regular',Helvetica,Arial,Tahoma,'Microsoft YaHei','San Francisco','微软雅黑','Hiragino Sans GB',STHeitiSC-Light; font-size: 12px; height: auto; margin: auto; overflow: hidden; text-align: left; word-break: break-all; word-wrap: break-word;"> <tbody style="margin: 0; padding: 0;"> <tr style="background-color: #393D49; height: 60px; margin: 0; padding: 0;"> <td style="margin: 0; padding: 0;"> <div style="color: #5EB576; margin: 0; margin-left: 30px; padding: 0;"><a style="font-size: 14px; margin: 0; padding: 0; color: #5EB576; text-decoration: none;" href="http://fly.layui.com/" target="_blank">Fly社区 - Layui前端框架官方社区</a></div> </td> </tr> <tr style="margin: 0; padding: 0;"> <td style="margin: 0; padding: 30px;"> <p style="line-height: 20px; margin: 0; margin-bottom: 10px; padding: 0;"> Hi，<em style="font-weight: 700;">{$nickname}</em>，请完成以下操作： </p> <div style=""> <a href="{$url}" style="background-color: #009E94; color: #fff; display: inline-block; height: 32px; line-height: 32px; margin: 0 15px 0 0; padding: 0 15px; text-decoration: none;" target="_blank">立即激活邮箱</a> </div> <p style="line-height: 20px; margin-top: 20px; padding: 10px; background-color: #f2f2f2; font-size: 12px;"> 如果该邮件不是由你本人操作，请勿进行激活！否则你的邮箱将会被他人绑定。 </p> </td> </tr> <tr style="background-color: #fafafa; color: #999; height: 35px; margin: 0; padding: 0; text-align: center;"> <td style="margin: 0; padding: 0;">系统邮件，请勿直接回复。</td> </tr> </tbody> </table></includetail></div>
xbs;

       $res = Mail::send($user->email,config('site_name').'激活邮件',$content);

       if($res){
            $this->success("已成功将激活链接发送到了您的邮箱，接受可能会稍有延迟，请注意查收。");
       }else{
            $this->error("发送失败，有事情找警察！！");
       }
    }

    public function upload()
    {
         // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){

            $info = $file->validate(['size'=>1567800,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');

            if($info){
               $name = $info->getFilename();

               $filePath = "uploads/".date("Ymd").'/'.$name;

               $user = new UserModel;

                $res = $user->update(['face'=>$filePath,'user_id'=>session('user_id')]);

                unlink(session('face'));

                session('face',$filePath);
                

                if($res){
                    $this->success('上传成功');
                }else{
                    $this->error('上传失败');
                }

            }else{
                // 上传失败获取错误信息
                $this->error($file->getError());
            }
        }
    }

    public function edit()
    {
        $postData = input('post.');

        $postData['user_id'] = session('user_id');

        $validate = Loader::validate('User');

        if(!$validate->scene('edit')->check($postData)){
            $this->error($validate->getError());
        }

        $user = new UserModel;

        $res = $user->update($postData);

        if($res){
            $this->success('提交成功');
        }else{
            $this->error('提交失败');
        }
    }

    public function set()
    {

        $user = UserModel::get(session('user_id'));

        $this->assign('user',$user);
        $this->assign('title',"用户设置");
    	return $this->fetch();
    }

    public function post()
    {
    	return $this->fetch();
    }

    public function sign()
    {
        
        $sign = (new Sign)->get_sign_info();

        if($sign['is_sign']){
            $this->error('今天已经签到过，请不要重复签到');
        }

        $res = Sign::get(['user_id'=>session('user_id')]);

        UserModel::where(['user_id'=>session('user_id')])->setInc('kiss',$sign['kiss']);

        if($res){

            $res = Sign::where(['user_id'=>session('user_id')])->update([
                'sign_date'=>date('Y-m-d'),
                'sign_time'=>time(),
                'sign_num'=>$sign['sign_num']+1
            ]);

        }else{

            $res = Sign::create([
                'user_id'=>session('user_id'),
                'sign_date'=>date('Y-m-d'),
                'sign_time'=>time(),
                'sign_num'=>1
            ]);
        }

        if($res){
            $this->success("签到成功");
        }else{
            $this->error("签到失败");
        }
    }
}